#include "cpu/exec/template-start.h"

#define instr sub

static void do_execute () {
	//如果源操作数和目的操作数是寄存器，更改val为正确的值
	if (op_src->type == OP_TYPE_REG ) {
		switch (op_src->reg) {
			case 0: op_src->val = cpu.eax; break;
			case 1: op_src->val = cpu.ecx; break;
			case 2: op_src->val = cpu.edx; break;
			case 3: op_src->val = cpu.ebx; break;
			case 4: op_src->val = cpu.esp; break;
			case 5: op_src->val = cpu.ebp; break;
			case 6: op_src->val = cpu.esi; break;
			case 7: op_src->val = cpu.edi; break;
			default: assert(0);
		}

	}
	if (op_dest->type == OP_TYPE_REG) {
		switch (op_dest->reg) {
			case 0: op_dest->val = cpu.eax; break;
			case 1: op_dest->val = cpu.ecx; break;
			case 2: op_dest->val = cpu.edx; break;
			case 3: op_dest->val = cpu.ebx; break;
			case 4: op_dest->val = cpu.esp; break;
			case 5: op_dest->val = cpu.ebp; break;
			case 6: op_dest->val = cpu.esi; break;
			case 7: op_dest->val = cpu.edi; break;
			default: assert(0);
		}
	}
	// Log("op_src->val = %x, op_dest->val = %x", op_src->val, op_dest->val);

	DATA_TYPE result = op_dest->val - op_src->val;
	OPERAND_W(op_dest, result);

	update_eflags_pf_zf_sf((DATA_TYPE_S)result);
	cpu.eflags.CF = result > op_dest->val;
	cpu.eflags.OF = MSB((op_dest->val ^ op_src->val) & (op_dest->val ^ result));

	print_asm_template2();
}

make_instr_helper(i2rm)
#if DATA_BYTE == 2 || DATA_BYTE == 4
make_instr_helper(si2rm)
#endif
make_instr_helper(r2rm)
make_instr_helper(rm2r)

#include "cpu/exec/template-end.h"
